package nameless.aistudy.api.role.user;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import nameless.aistudy.common.result.Result;
import nameless.aistudy.domain.service.user.UserAuthService;
import nameless.aistudy.domain.service.user.UserCheckInService;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
public class UserCheckinController {

    final UserCheckInService userCheckInService;
    final UserAuthService userAuthService;

    public UserCheckinController(
            UserCheckInService userCheckInService,
            UserAuthService userAuthService
    ) {
        this.userCheckInService = userCheckInService;
        this.userAuthService = userAuthService;
    }

    @Operation(
            summary = "用户打卡",
            description = "返回连续打卡天数"
    )
    @PostMapping("/checkin")
    public Result<Integer> checkin(
            HttpServletRequest request
    ) {
        int userId = userAuthService.getUserIdFromRequest(request);
        if (userCheckInService.checkIn(userId)) {
            int days =  userCheckInService.getContinuousCheckInDays(userId);
            return Result.success(days);
        }
        return Result.fail();
    }

    @Operation(
            summary = "查询用户打卡连续打卡天数"
    )
    @GetMapping("/statistics/checkin")
    public Result<Integer> statisticsCheckin(@RequestParam("userId") int userId) {
        int days =  userCheckInService.getContinuousCheckInDays(userId);
        return Result.success(days);
    }
}
